package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.RoleDto;
import com.itheima.admin.dto.RoleIdDto;
import com.itheima.admin.pojo.Role;
import com.itheima.admin.service.IRoleService;
import com.itheima.admin.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @PostMapping("/list")
    @ApiOperation("分页查询角色列表")
    public PageVo<RoleVo> queryByPage(@RequestBody PageDto pageDto){
        return roleService.queryByPage(pageDto);
    }
    @GetMapping("/listall")
    @ApiOperation("查询所有角色")
    public List<RoleVo> queryAllByPage(){
       return roleService.queryAllByPage();
    }
    @PostMapping("/add")
    @ApiOperation("添加角色")
    public Result addRole(@RequestBody RoleDto roleDto){
        boolean result = roleService.addRole(roleDto);
        return new Result(result,result?"添加角色成功":"添加角色失败",null);
    }
    @DeleteMapping("/{id}")
    @ApiOperation("删除角色")
    public Result deleteRole(@PathVariable("id") String id){
        boolean result = roleService.deleteRole(id);
        return new Result(result,result?"删除角色成功":"删除角色失败",null);
    }
    @PostMapping("/auth")
    @ApiOperation("授权角色")
    public Result roleAuth(@PathVariable Map map){
        boolean result = roleService.roleAuth(map);
        return new Result(result,result?"授权角色成功":"授权角色失败",null);
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询角色")
    public RoleIdDto queryById(@PathVariable String id){
        return roleService.queryById(id);
    }
    @PutMapping("/edit")
    @ApiOperation("修改角色")
    public Result updateRole(@RequestBody RoleIdDto roleIdDto){
        boolean result = roleService.updateRole(roleIdDto);
        return new Result(result,result?"修改角色成功":"修改角色失败",null);
    }
    @GetMapping("/{id}/mudules")
    @ApiOperation("查询角色对应的模块")
    public Result queryRoleModules(@PathVariable String id){
        Result result = roleService.queryRoleModules(id);
        return new Result(result.isFlag(),result.isFlag()?"查询角色对应模块成功":
                "查询角色对应模块失败",result.getData());
    }
}
